package bavaria.hightech.banking;

import bavaria.hightech.exceptions.NoSuchElementException;

public class Buchungsliste <Element> {
	
	private ListElement<Element> anfang;
	private ListElement<Element> position;
	private int size = 0;
	
	public Buchungsliste(ListElement<Element> element){
		anfang = element;
		anfang.next = null;
	}
	
	public void add(ListElement<Element> element){
		resetIteration();
		if(anfang.next == null){
			anfang.next = element;
			position = anfang.next;
			position.next = null;
		}
		else if(iterator().hasNext() == false){
			position.next = element;
			position = position.next;
			position.next = null;
		}
		else if(iterator().hasNext()){
			while(iterator().hasNext())
				next();
			position.next = element;
			position = position.next;
			position.next = null;
		}
		size++;
	}

	public void next() {
		position = iterator().next();
		
	}
	
	public void resetIteration(){
		position = anfang;
	}
	
	public void clear(){
		resetIteration();
		position.next = null;
		anfang.next = null;
	}



	public ListElement<Element> getAnfang() {
		return anfang;
	}

	public ListElement<Element> getPosition() {
		return position;
	}
	
	public ListIterator iterator(){
		return new ListIterator();
	}
	
	
	

private class ListIterator  { 
     
    public boolean hasNext() { 
    	return position.next != null;}

    public ListElement<Element> next() {
      if (position == null) 
         throw new NoSuchElementException("Gibbet nich!");
      return position.next;
    }


		
	}  }


